﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class QueryFilter
    {
        public static void BuildFilter(StringBuilder sb, string field, string fieldValue, Type type, string op)
        {
            try
            {
                if ( sb.Length > 0 )
                    sb.AppendFormat(" {0} ", "AND");

                #region Switch Type
                if (op == "Contains")
                {
                    #region Contains
                    if (type == typeof(String))
                    {
                        sb.AppendFormat("{0}.ToUpperInvariant()." + op + "(\"{1}\")", field, fieldValue.ToUpperInvariant());
                    }
                    if (type == typeof(Int32))
                    {
                        //fieldValue = comma delimited string => examole: "2, 4, 18"
                        var a = new int[] {2, 4}.AsEnumerable().Contains(2);
                        //var query = "new int[] {" + fieldValue + "}.Contains(" + field + ")";
                        var query = "new string[] {\"200\", \"4\"}.Contains( " + field + ".ToUpperInvariant() )";
                        sb.Append(query);
                    }
                    if (type == typeof(Enum))
                    {
                        sb.AppendFormat("{0} " + op + " \"{1}\"", field, fieldValue);
                    }
                    #endregion
                }
                else
                {
                    if (type == typeof(Int64))
                    {
                        sb.AppendFormat("{0} " + op + " Convert.ToInt64(\"{1}\")", field, fieldValue);
                    }
                    else if (type == typeof(Int32))
                    {
                        sb.AppendFormat("{0} " + op + " Convert.ToInt32(\"{1}\")", field, fieldValue);
                    }
                    else if (type == typeof(Decimal))
                    {
                        sb.AppendFormat("{0} " + op + " Convert.ToDecimal(\"{1}\")", field, fieldValue);
                    }
                    else if (type == typeof(DateTime))
                    {
                        sb.AppendFormat("{0}.Date " + op + " Convert.ToDateTime(\"{1}\")", field, fieldValue);
                    }
                    else if (type == typeof(DateTime?))
                    {
                        sb.AppendFormat("{0}.Value.Date " + op + " Convert.ToDateTime(\"{1}\")", field, fieldValue);
                    }
                    else if (type == typeof(Boolean))
                    {
                        if (fieldValue == "1" || fieldValue.ToLower() == "true")
                            sb.AppendFormat("{0}", field);
                        else
                            sb.AppendFormat("!{0}", field);
                    }
                    else if (type == typeof(Enum))
                    {
                        sb.AppendFormat("{0} " + op + " \"{1}\"", field, fieldValue);
                    }
                    else if (type == typeof(String))
                    {
                        sb.AppendFormat("{0}.ToUpperInvariant() " + op + " \"{1}\"", field, fieldValue.ToUpperInvariant());
                    }
                }

                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public class EqualityOperators
        {
            /// <summary>
            /// 
            /// </summary>
            public const string Equal = "==";
            /// <summary>
            /// 
            /// </summary>
            public const string NotEqual = "!=";
            /// <summary>
            /// 
            /// </summary>
            public const string GreaterThan = ">";
            /// <summary>
            /// 
            /// </summary>
            public const string GreaterThanOrEqual = ">=";
            /// <summary>
            /// 
            /// </summary>
            public const string LessThan = "<";
            /// <summary>
            /// 
            /// </summary>
            public const string LessThanOrEqual = "<=";
            /// <summary>
            /// 
            /// </summary>
            public const string In = "In";
            /// <summary>
            /// 
            /// </summary>
            public const string NotIn = "Not In";
            /// <summary>
            /// 
            /// </summary>
            public const string Contains = "Contains";
            /// <summary>
            /// 
            /// </summary>
            public const string NotContains = "";
            /// <summary>
            /// 
            /// </summary>
            public const string StartsWith = "StartsWith";
            /// <summary>
            /// 
            /// </summary>
            public const string NotStartsWith = "";
            /// <summary>
            /// 
            /// </summary>
            public const string EndsWith = "";
            /// <summary>
            /// 
            /// </summary>
            public const string NotEndsWith = "";
            /// <summary>
            /// 
            /// </summary>
            public const string IsNull = "string.IsNullOrEmpty( )";
            /// <summary>
            /// 
            /// </summary>
            public const string IsNotNull = "";
        }
    }
}